import java.util.Arrays;
import java.util.Scanner;

public class Java7 {

    public static int find(int[] array,int val) {
        for (int i = 0; i < array.length; i++) {
            if (array[i] == val) {
                return i;
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        //查找
        int[] array = {1,2,3,4};
        int  ret = find(array,5);
        System.out.println(ret);
    }

    public static void main29(String[] args) {
        int[] array1 = {1,2,3,4};
        //克隆：产生一个副本
        int[] array2 = array1.clone();
        System.out.println(Arrays.toString(array1));
        System.out.println(Arrays.toString(array2));
        System.out.println("========");
        array2[0] = 199;
        System.out.println(Arrays.toString(array1));
        System.out.println(Arrays.toString(array2));
    }

    public static void main28(String[] args) {
        int[] array1 = {1,2,3,4};
        int[] array2 = {1,2,31,4};
        //System.out.println(array1 == array2);
        //判断数组里的值是不是一样 返回值为true/false
        System.out.println(Arrays.equals(array1,array2));

        int[] array3 = new int[10];//动态初始化
        //赋值
        Arrays.fill(array3,1,3,-1);
        System.out.println(Arrays.toString(array3));
    }

    public static void main27(String[] args) {
        int[] array = {1,2,3,4,6};
        int[] ret = Arrays.copyOfRange(array,1,3);//[1,3)
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(ret));
    }

    public static void main26(String[] args) {
        int[] array = {1,2,3,4};
        //方法拷贝                                        缩小
        int[] ret = Arrays.copyOf(array,array.length/2);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(ret));
        System.out.println("========");
        ret[0] = 199;
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(ret));
    }

    public static void main25(String[] args) {
        int[] array = {1,2,3,4};
        int[] copy = new int[array.length];
        //System.arraycopy(array,0,copy,0,array.length);
        //指定范围拷贝
        System.arraycopy(array,1,copy,0,array.length-1);

        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(copy));
        System.out.println("========");
        copy[0] = 199;
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(copy));
    }

    public static void main24(String[] args) {
        int[] array = {1,2,3,4};
        //方法拷贝                                         扩容
        int[] ret = Arrays.copyOf(array,array.length*2);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(ret));
        System.out.println("========");
        ret[0] = 199;
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(ret));
    }

    public static void main23(String[] args) {
        //最简单的拷贝
        int[] array = {1,2,3,4};

        int[] array2 = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            array2[i] = array[i];
        }
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(array2));
        System.out.println("========");
        array2[0] = 199;
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(array2));
    }

    public static void main22(String[] args) {
        //不叫拷贝 因为此时只有一个数组对象
        int[] array = {1,2,3,4};
        System.out.println(Arrays.toString(array));
        int[] array2 = array;
        System.out.println(Arrays.toString(array2));
    }

    public static String myToString(int[] array) {
        if (array ==  null) {
            return null;
        }
        String ret = "[";
        for (int i = 0; i < array.length; i++) {
            ret += array[i];
            if (i != array.length-1) {
                ret += ",";
            }
        }
        ret = ret + "]";
        return ret;
    }

    public static void main21(String[] args) {
        //数组转字符串
        int[] array = {1,2,3,4,5,6};
        System.out.println(myToString(array));
        System.out.println(myToString(null));
    }

    public static int[] grow3(int[] array) {
        //                     java里可以是变量
        int[] tmpArray = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            tmpArray[i] = array[i] * 2;
        }
        return tmpArray;
    }

    public static void main20(String[] args) {
        //将数组array的值扩大2倍
        int[] array = {1,2,3,4,5,7,8};
        int[] ret = grow3(array);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(ret));
    }

    public static void grow2(int[] array) {
        for (int i = 0; i < array.length; i++) {
            array[i] = array[i] * 2;
        }
    }
    public static void main19(String[] args) {
        //修改main18
        int[] array = {1,2,3,4,5,7,8};
        grow2(array);
        System.out.println(Arrays.toString(array));
    }

    public static int[] grow(int[] array) {
        for (int i = 0; i < array.length; i++) {
            array[i] = array[i] * 2;
        }
        return array;
    }
    public static void main18(String[] args) {
        //将数组array的值扩大2倍
        int[] array = {1,2,3,4,5,7,8};
        int[] ret = grow(array);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(ret));
    }

    public static int[] func10() {
        int[] tmpArr = {10,11,12};
        return tmpArr;
    }
    public static void main17(String[] args) {
        //int[] array = {1,3,5,7,9};
        int[] array = func10();
        System.out.println(Arrays.toString(array));
    }
    
    public static void function(int b) {
        b = 20;
    }

    public static void main16(String[] args) {
        int a = 10;
        function(a);
        System.out.println(a);
    }

    public static void func1(int[] array) {
        //形参
        array = new int[]{15,16,17};
    }

    public static void func2(int[] array) {
        array[0] = 999;
    }

    public static void main15(String[] args) {
        //实参
        int[] array1 = {1,2,3,4};
        //func1(array1);
        func2(array1);
        System.out.println(Arrays.toString(array1));
    }

    public static void main14(String[] args) {
        int[] array1 = {1,2,3,4};
        int[] array2 = {11,22,33,44};
        array1 = array2;
        array1[0] = 1888;
        System.out.println(Arrays.toString(array1));
        System.out.println(Arrays.toString(array2));
    }

    public static void main13(String[] args) {
        int[] array1 = {1,2,3,4};
        array1[0] = 99;

        int[] array2 = array1;
        array2[0] = 100;

        System.out.println(Arrays.toString(array1));
        System.out.println(Arrays.toString(array2));
    }

    public static void main12(String[] args) {
        int[] array4 = null;
        System.out.println(array4[0]);

    }

    public static void main11(String[] args) {
        int[] array1 = {1,2,3,4};//初始化
        //array1这个引用 指向了一个对象
        int[] array2 = new int[]{1,2,3,4};//初始化
        int[] array3 = new int[10];//默认

        int[] array4 = null;
        System.out.println(array4);

        int a = 0;
        System.out.println(a);

    }


    //============================================
    public static void printArray(int[] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+" ");
        }
        System.out.println();
    }

    public static void main10(String[] args) {
        //实现一个方法 printArray, 以数组为参数, 循环访问数组中的每个元素, 打印每个元素的值.
        int[] array = new int[100];
        for (int i = 0; i < array.length; i++) {
            array[i] = i+1;
        }
        printArray(array);
    }

    public static void main9(String[] args) {
        //创建一个 int 类型的数组, 元素个数为 100, 并把每个元素依次设置为 1 - 100
        int[] array = new int[100];
        for (int i = 0; i < array.length; i++) {
            array[i] = i+1;
        }
        System.out.println(Arrays.toString(array));
    }
//=======================================================
    public static void printarray(int a[]) {
        for (int x:a){
            System.out.println(x);
        }
    }

    public static void main8(String[] args) {
        //实现一个方法 printArray, 以数组为参数, 循环访问数组中的每个元素, 打印每个元素的值.
        int [] array = {1,2,3};
        printarray(array);
    }

    public static void main7(String[] args) {
        //创建一个 int 类型的数组,元素个数为 100, 并把每个元素依次设置为 1 - 100
        int[] array = new int [100];
        for (int i = 0; i < 100; i++) {
            array[i] = i+1;
        }
        String ret = Arrays.toString(array);
        System.out.println(Arrays.toString(array));
    }

    public static void hnt(int a,char pos1,char pos2,char pos3) {
        if (a == 1) {
            move(pos1,pos3);
            return;
        }
        hnt(a-1,pos1,pos3,pos2);
        move(pos1,pos3);
        hnt(a-1,pos2,pos1,pos3);
    }

    public static void move(char pos1,char pos2) {
        System.out.print(pos1+"->"+pos2+" ");
    }

    public static void main6(String[] args) {
        //递归求解汉诺塔问题
        hnt(1,'A','B','C');
        System.out.println();
        hnt(2,'A','B','C');
        System.out.println();
        hnt(3,'A','B','C');
    }

    public static int feib(int n) {
        if (n == 1 || n == 2) {
            return 1;
        }
        return feib(n-1) + feib(n - 2);
    }

    public static void main5(String[] args) {
        //递归求斐波那契数列的第 N 项
        int x = feib(6);
        System.out.println(x);
    }

    public static void sunx(int n) {
        if (n <= 9) {
            System.out.println(n);
            return;
        }
        sunx(n / 10);
        System.out.println(n % 10);
    }

    public static void main4(String[] args) {
        //按顺序打印一个数字的每一位(例如 1234 打印出 1 2 3 4)
        sunx(1234);
    }

    public static int qiuh(int n) {
        int sum = 0;
        if (n == 1) {
            return 1;
        }
        return n + qiuh(n-1);
    }

    public static void main3(String[] args) {
        //递归求 1 + 2 + 3 + ... + 10
        int s = qiuh(10);
        System.out.println(s);
    }

    public static int jiec(int n) {
        if (n == 1) {
            return 1;
        }
        return n*jiec(n-1);
    }

    public static void main2(String[] args) {
        //递归求 N 的阶乘
        int c = jiec(5);
        System.out.println(c);
    }

    public static int sum(int n) {
        int i = 0;
        while (n != 0) {
            i = n%10;
            return (i+sum(n/10));
        }
        return 0;
    }

    public static void main1(String[] args) {
        //输入一个非负整数，返回组成它的数字之和
        int s = sum(123);
        System.out.println(s);
    }

}
